package de.tiemannjan.android.myrun;

import java.text.DecimalFormat;

import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import de.tiemannjan.android.util.LocationTracker;
import de.tiemannjan.android.util.StopWatch;


public class MyRunActivity extends Activity implements OnClickListener {
	private Button mBtnStart;
	private StopWatch mStopWatch;
	private TextView mTvTime;
	private TextView mTvDistance;
	private Handler mHandler;
	private Runnable mUpdateTimeTask;
	private LocationManager mLocationManager;
	private LocationTracker mLocationTracker;
	private LocationListener mLocationListener;
	private boolean mRunning;
	
	private final int TIME_UPDATE_INTERVAL = 100;
	
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        this.mBtnStart = (Button) findViewById(R.id.button_start);
        this.mBtnStart.setOnClickListener(this);
        	
     	this.initTimeHandling();
     	this.initLocationHandling();
     	
     	
     	mRunning = false;
     	
    }
    
    public void onStop(){
    	super.onStop();
    	this.mLocationManager.removeUpdates(this.mLocationListener);
    }
    
    public void onDestroy(){
    	super.onDestroy();
    	this.mLocationManager.removeUpdates(this.mLocationListener);
    }
    
    private void initTimeHandling(){
    	mStopWatch = new StopWatch();
        
        mTvTime = (TextView) findViewById(R.id.time);
        mHandler = new Handler();
        
        
        // Handler used to update the stop watch
        mUpdateTimeTask = new Runnable() {
     	   public void run() {
     	       mTvTime.setText(mStopWatch.getTimeFormatted());
     		   mHandler.postDelayed(mUpdateTimeTask, TIME_UPDATE_INTERVAL);
     	   }
     	};
    }
    
    private void initLocationHandling(){
    	this.mLocationTracker = new LocationTracker();
     	
     	// initialize distance text view
     	this.mTvDistance = (TextView) findViewById(R.id.distance);
     	
     	// Acquire a reference to the system Location Manager
     	this.mLocationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);

     	// Define a listener that responds to location updates
     	this.mLocationListener = new LocationListener() {
     	    public void onLocationChanged(Location location) {
     	    	// Called when a new location is found by the network location provider.
     	    	handleLocation(location);
     	    }

     	    public void onStatusChanged(String provider, int status, Bundle extras) {}

     	    public void onProviderEnabled(String provider) {}

     	    public void onProviderDisabled(String provider) {}
     	  };

     	// Register the listener with the Location Manager to receive location updates
     	mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mLocationListener);
    }    
    
    public void onClick(View v){
    	CharSequence text;
    	
    	if (v==mBtnStart){
    		text = mBtnStart.getText();
    		if (getString(R.string.start) == text){
    			start();
    		} else {
    			stop();
    		}
    	}
    }
    
    private void handleLocation(Location location){
    	this.mLocationTracker.addLocation(location);
    	if (this.getRunning()){
    		double distance = this.mLocationTracker.getDistanceKm();
	    	DecimalFormat df = new DecimalFormat("#0.###");
	    	this.mTvDistance.setText(df.format(distance));
//	    	this.mTvDistance.setText(location.toString());
    	}
    }
    
    private void start(){
    	mRunning = true;
    	startStopWatch();
		mLocationTracker.start();
		mBtnStart.setText(R.string.stop);
    }
    
    private void stop(){
    	mRunning = false;
    	stopStopWatch();
		mLocationTracker.stop();
		mBtnStart.setText(R.string.start);
    }
    
    private boolean getRunning(){
    	return this.mRunning;
    }
    
    private void startStopWatch(){
    	mStopWatch.start();
		mHandler.removeCallbacks(mUpdateTimeTask);
        mHandler.postDelayed(mUpdateTimeTask, TIME_UPDATE_INTERVAL);
    }
    
    private void stopStopWatch(){
    	mStopWatch.stop();
		mHandler.removeCallbacks(mUpdateTimeTask);
    }
}